package cn.lzm.basic.service.impl;

import cn.lzm.basic.mapper.BaseMapper;
import cn.lzm.basic.query.BaseQuery;
import cn.lzm.basic.service.IBaseService;
import cn.lzm.basic.util.PageList;
import cn.lzm.org.domain.Employee;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

public class BaseServiceImpl<T> implements IBaseService<T> {
    @Autowired
    private BaseMapper<T> baseMapper;
    @Override
    public void add(T t) {
        baseMapper.add(t);
    }

    @Override
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    public void remove(Long id) {
        baseMapper.delete(id);
    }

    @Override
    public List<T> queryAll() {
        return baseMapper.findAll();
    }

    @Override
    public T queryById(Long id) {
        return (T) baseMapper.findById(id);
    }

    @Override
    public Integer queryByCount(BaseQuery query) {
        return baseMapper.queryByCount(query);
    }

    @Override
    public PageList<T> queryPage(BaseQuery query) {
        if (!StringUtils.isNullOrEmpty(query.getKeyword())) {
            query.setKeyword(query.getKeyword().trim());
        }
        Integer totals = baseMapper.queryByCount(query);
        if (null == totals || 0 == totals) {
            return new PageList<>(0, new ArrayList<>());
        }
        // 查询数据
        List<T> employees = baseMapper.queryByPage(query);
//        System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&");
//        departments.forEach(System.out::println);
//        System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&");
        //方到返回里面
        return new PageList<T>(totals, employees);
    }

    @Override
    public void batchDel(List<Long> ids) {
        baseMapper.batchDel(ids);
    }
}
